package com.pro.controller;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.pro.pojo.JsMicrocircle;
import com.pro.pojo.JsUser;
import com.pro.pojo.JsonData;
import com.pro.service.JsMicrocircleService;
import com.pro.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * ClassName:JsMicrocircleController
 * Package:com.pro.controller
 * Description:
 *
 * @Date:2021/11/28 下午 8:33
 * @Author: WYH
 */
@Controller
@RequestMapping("/microcircle")
public class JsMicrocircleController {

    @Autowired
    private JsMicrocircleService jsMicrocircleService;

    @RequestMapping("/getTable")
    @ResponseBody
    public JsonData getTable(String acticleCategroyId,String artcleTitle ,String trueName,String account){
        acticleCategroyId = acticleCategroyId != null && acticleCategroyId != " " ? "%"+acticleCategroyId+"%":"%%";
        artcleTitle = artcleTitle != null && artcleTitle != " " ? "%"+artcleTitle+"%":"%%";
        trueName = trueName != null && trueName != " " ? "%"+trueName+"%":"%%";
        account = account != null  && account != " " ? "%"+account+"%":"%%";
        List<JsMicrocircle> table = jsMicrocircleService.getTable(acticleCategroyId,artcleTitle,trueName,account);
        int count = jsMicrocircleService.count(acticleCategroyId,artcleTitle, trueName, account);
        return table!=null ? JsonData.buildSuccess(table,count) : JsonData.buildError("数据格式异常");
    }

    @RequestMapping("/deleteMicro")
    @ResponseBody
    public JsonData deleteMicro(Integer microcircleId,Integer createId){
        Boolean flag=jsMicrocircleService.deleteMicro(microcircleId,createId);
        return flag!=false ? JsonData.buildSuccess(flag) : JsonData.buildError("服务器异常");
    }

    @RequestMapping("/getMicMsg")
    @ResponseBody
    public JsonData getMicMsg(Integer microcircleId,Integer createId){
        JsMicrocircle msg=jsMicrocircleService.getMicMsg(microcircleId,createId);
        return msg!=null ? JsonData.buildSuccess(msg) : JsonData.buildError("服务器异常");
    }

    @RequestMapping("/addMic")
    @ResponseBody
    public JsonData addMic(Integer userId, Integer commodityId, String article, Integer parentId){
        //@Param("userId")Integer userId,@Param("commodityId")Integer commodityId,@Param("text")String text,@Param("parentId")Integer parentId,@Param("commentDate")String commentDate
        JsMicrocircle micro=new JsMicrocircle();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        Date dateTime = null;
        try {
            dateTime = df.parse(df.format(new Date()));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        micro.setUserId(userId);
        micro.setCommodityId(commodityId);
        micro.setArticle(article);
        micro.setParentId(parentId);
        micro.setCreateDate(dateTime);
        Boolean flag=jsMicrocircleService.addMic(micro);
        return flag!=false ? JsonData.buildSuccess(flag) : JsonData.buildError("服务器异常");
    }

}
